//author Dreena Biswas
package addressbook2;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import au.com.bytecode.opencsv.*;

public class Model {
	
	//ADD
	public void addContact(String fName, String lName, String group, String homeAdd, String compAdd, String personalMobile, String personalLand, String profMobile, String profLand, String homePage, String emailId, String photo, String date){
	
		String csv = "C:\\output.csv";
		CSVWriter writer;
		
		try {			
			writer = new CSVWriter(new FileWriter(csv,true));
			String [] contact = {fName, lName, group, homeAdd, compAdd, personalMobile, personalLand, profMobile, profLand, homePage, emailId, photo, date};
			 
			writer.writeNext(contact);
			writer.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		 
		
	}// end of ADD
	

	// DELETE
	public void deleteContact(String fName, String lName) throws IOException{
		String csvFilename = "C:\\output.csv";
		String csvFiletemp = "C:\\output_temp.csv";
		CSVWriter writer;
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		writer = new CSVWriter(new FileWriter(csvFiletemp));
		while((row = csvReader.readNext()) != null) {
			//System.out.println(row[0]);
			if(!(row[0].equals(fName) && row[1].equals(lName))){
				String [] contact = {row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]};
				
				System.out.println(contact[0]);
				writer.writeNext(contact);
			}
			
		}
			writer.close();
			csvReader.close();
			File file1 = new File("c:\\output.csv");
			file1.delete();
			File file2 = new File("c:\\output_temp.csv");
			file2.renameTo(file1);						
	} // end of DELETE
	
	
	// DISPLAY
	public ArrayList<String> displayContact(String fName, String lName) throws IOException{
		String csvFilename = "C:\\output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> cont = new ArrayList<String>();
		
		while((row = csvReader.readNext()) != null) {
			if(row[0].equals(fName) && row[1].equals(lName))
			{
				cont.add(new StringBuilder().append("First Name :     ").append(row[0]).toString());
				cont.add(new StringBuilder().append("Last Name :     ").append(row[1]).toString());
				cont.add(new StringBuilder().append("Group :     ").append(row[2]).toString());
				cont.add(new StringBuilder().append("Home Address :     ").append(row[3]).toString());
				cont.add(new StringBuilder().append("Company Address :     ").append(row[4]).toString());
				cont.add(new StringBuilder().append("Personal Mobile :     ").append(row[5]).toString());
				cont.add(new StringBuilder().append("Personal Land :     ").append(row[6]).toString());
				cont.add(new StringBuilder().append("Professional Mobile :     ").append(row[7]).toString());
				cont.add(new StringBuilder().append("Professional Land :     ").append(row[8]).toString());
				cont.add(new StringBuilder().append("Home Page Link :     ").append(row[9]).toString());
				cont.add(new StringBuilder().append("Email id :     ").append(row[10]).toString());
				cont.add(new StringBuilder().append("Photo :     ").append(row[11]).toString());
				cont.add(new StringBuilder().append("Date :     ").append(row[12]).toString());
			}	
			}
		//...
		csvReader.close();
		return cont;
	} // end of DISPLAY
	
	
	//SORT ALPHA
	public void sortAlpha(){
			
	} //end of SORT ALPHA
	
	
	// SORT GROUP
	public ArrayList<String> sortGroup(String group) throws IOException{
		String csvFilename = "C:\\output.csv";
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		ArrayList<String> contarr = new ArrayList<String>();
		while((row = csvReader.readNext()) != null) {
			if(row[2].equals(group))
			{
				String ex = (row[0]
			              + " " + row[1]);
			contarr.add(ex);}
		}
		
		//...
		csvReader.close();
		return(contarr);
	} // end of SORT GROUP
	
	
	//SORT DATE
	public void sortDate(){
		
	} //end of SORT DATE

	
	//EDIT CONTACT
	/*public void editContact(String fName, String lName, String group,
			String homeAdd, String compAdd, String personalMobile,
			String personalLand, String profMobile, String profLand,
			String homePage, String emailId, String photo, String date) throws IOException {
		String csvFilename = "C:\\output.csv";
		String csvFiletemp = "C:\\output_temp.csv";
		CSVWriter writer;
		CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
		String[] row = null;
		writer = new CSVWriter(new FileWriter(csvFiletemp));
		while((row = csvReader.readNext()) != null) {
			//System.out.println(row[0]);
			if(!(row[0].equals(fName) && row[1].equals(lName))){
				String [] contact = {row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]};
				
				System.out.println(contact[0]);
				writer.writeNext(contact);
			}
			else{
				String [] contact = {fName, lName, group, homeAdd, compAdd, personalMobile, personalLand, profMobile, profLand, homePage, emailId, photo, date};
				
				System.out.println(contact[0]);
				writer.writeNext(contact);
				
			}
			
		}
			writer.close();
			csvReader.close();
			File file1 = new File("c:\\output.csv");
			file1.delete();
			File file2 = new File("c:\\output_temp.csv");
			file2.renameTo(file1);
		
	} // end of EDIT */
	
	public ArrayList<String> editContact(String fName, String lName){
		
	}

}
